using System;
using System.Collections.Generic;
using System.Text;

using ILIMS.Core;

namespace ILIMS.Data.Dao
{	
	public class AbstractDao<Type> : IDao<Type> {
		
		public List<Type> GetAll() {
			return GetByCriteria();
		}
		
		public Type GetId(int id) {
			 return (Type) Session.Load (typeof(Type) , id);
		}
		
		public Type Save(Type obj) {
			Session.Save(obj);
			return obj;
		}
		
		public Type SaveOrUpdate(Type obj) {
			Session.SaveOrUpdate(obj);
			return obj;
		}
		
		public void Delete(Type obj) {
			Session.Delete(obj);
		}
		
        public void Flush() {
			Session.Flush();
        }
		
		/// <summary>
		/// Get values with criteria
		/// </summary>
		/// <param name="criterion">criteria to get values by</param>
		public List<Type> GetByCriteria ( params NHibernate.Expression.ICriterion[] criterion )
		{
		    NHibernate.ICriteria criteria = Session.CreateCriteria ( typeof ( Type ) );

		    foreach ( NHibernate.Expression.ICriterion criterium in criterion )
		    {
			criteria.Add ( criterium );
		    }

		    return criteria.List<Type> ( ) as List<Type>;
		}
		
		/// <summary>
		/// Exposes the ISession used within the DAO.
		/// </summary>
		private NHibernate.ISession Session
		{
		    get
		    {
			return SessionManager.Instance.GetSession ( );
		    }
		}
	}
}
